package a66weding.com.jiehuntong.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Scroller;

import a66weding.com.jiehuntong.R;

/**
 *
 */
public class SwipteLayout extends FrameLayout {
    private LinearLayout countView;
    private Button menuView;
    private Scroller scroller;

    private int countWidth;
    private int countHeight;
    private int menuWidth;
    private int menuHeight;

    private OnItemStateChanged onItemStateChanged;

    private boolean isopen;
    private boolean isIntercept;
    private View deviderView;
    private int deviderHeight;


    public SwipteLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        scroller=new Scroller(context);

    }

    public boolean isopen() {
        return isopen;
    }

    public void setIsopen(boolean isopen) {
        this.isopen = isopen;
    }

    //1.获取到两个子View对象:在Activity创建好所有视图对象后，会调用onFinishInflate（）方法
    //可获取子视图对象
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();


        countView = (LinearLayout) getChildAt(0);
        deviderView = countView.findViewById(R.id.view_divider);

        menuView= (Button) getChildAt(1);

    }


    //获取子视图对象的宽高：在onMeasure()方法中可获取
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        countWidth=countView.getMeasuredWidth();
        deviderHeight = deviderView.getMeasuredHeight();
        countHeight=countView.getMeasuredHeight()-deviderHeight;
        menuWidth=menuView.getMeasuredWidth();
        menuHeight=menuView.getMeasuredHeight();


    }

    //重写onLayout对两个子View进行布局
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        menuView.layout(countWidth,deviderHeight,countWidth+menuWidth, countHeight+deviderHeight);
    }

    int lastX,lastY;
    int startX,startY;
    int totalX,totalY;
    //给Item添加触摸监听
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int eventX= (int) event.getRawX();
        int eventY= (int) event.getRawY();

         switch (event.getAction()){
                     case MotionEvent.ACTION_DOWN:
                         lastX= eventX;
                         lastY= eventY;
                         startX=eventX;
                         startY=eventY;
                         break;

                     case MotionEvent.ACTION_MOVE:
                         int dx=eventX-lastX;
                         int dy=eventY-lastY;
                         int scrolltoX=getScrollX()-dx;
                         int scrolltoY=getScrollY()-dy;
                         //限定范围
                         if (scrolltoX > menuWidth) {
                             scrolltoX=menuWidth;
                         }
                         if (scrolltoX < 0) {
                             scrolltoX=0;
                         }
                         scrollTo(scrolltoX,0);
                         lastX= eventX;
                         lastY= eventY;

                         //事件反拦截条件
                         totalX=Math.abs(eventX-startX);
                         totalY=Math.abs(eventY-startY);
                         if (totalX > totalY && totalX > 50) {
                             //实行反拦截
                             getParent().requestDisallowInterceptTouchEvent(true);
                             isIntercept=true;
                         }else{
                             isIntercept=false;
                         }
                         break;
             case MotionEvent.ACTION_CANCEL:
             case MotionEvent.ACTION_UP:
                         if (getScrollX() > menuWidth / 2) {
                             openMenu();
                         }else{
                             closeMenu();
                         }

                      break;
                 }
        return true;
    }

    //打开菜单
    public void openMenu() {
        scroller.startScroll(getScrollX(),getScrollY(),menuWidth-getScrollX(),0);
        invalidate();
        this.setIsopen(true);
        if (onItemStateChanged != null) {
            onItemStateChanged.onOpen(this);
        }
    }
    //关闭菜单
    public void closeMenu() {
        scroller.startScroll(getScrollX(),getScrollY(),-getScrollX(),0);
        invalidate();
        if (onItemStateChanged != null) {
            onItemStateChanged.onClose(this);
        }
    }

//调用invalidate()时需要重写computeScroll()
    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(),scroller.getCurrY());
            invalidate();
        }
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean isIntercept=false;
        int eventX= (int) event.getRawX();
        int eventY= (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = eventX;
                startY = eventY;
                if (onItemStateChanged != null) {
                    onItemStateChanged.onDown(this);
                }
                break;
            case MotionEvent.ACTION_MOVE:

                //事件拦截条件
                totalX = Math.abs(eventX - startX);
                totalY = Math.abs(eventY - startY);
                if (totalX > totalY && totalX > 132) {
                        isIntercept = true;
                    if (onItemStateChanged != null) {
                        onItemStateChanged.onDown(this);
                    }
                }
//
                break;

            case MotionEvent.ACTION_UP:
                break;
        }

        return super.onInterceptTouchEvent(event);

    }

    public void setOnItemStateChanged(OnItemStateChanged onItemStateChanged) {
        this.onItemStateChanged = onItemStateChanged;
    }



    public interface OnItemStateChanged{
        public void onOpen(SwipteLayout layout);//当item打开时
        public void onClose(SwipteLayout layout);//当item关闭时
        public void onDown(SwipteLayout layout);//当手指触摸某个item时
    }
}
